<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" placeholder="请输入搜索内容" bgColor="#e8e8e8" radius=18 cancelButton='none'
        clearButton="auto">
      </uni-search-bar>
    </view>

    <!-- 搜索建议列表 -->
    <view v-if="kw !==''">
      <view class="search-item" v-for="(item,index) in searchResults" :key="index" @click="search(item)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题区域 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash-filled" @click="cleanHistory"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,index) in historys" :key="index" @click="gotoGoodsList(item)"></uni-tag>
      </view>

      <!-- </view> -->
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 延时器的 timerId
        timer: null,
        // 搜索关键值
        kw: '',
        // 搜索结果列表
        searchResults: [],
        // 搜索关键词的历史记录
        historyList: []
      };
    },
    computed: {
      onLoad() {
        this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
      },
      historys() {
        return [...this.historyList].reverse()

      }
    },
    methods: {
      gotoGoodsList(kw) {
        uni.navigateTo({
          url: '/subpkg/goods-list/goods-list?query=' + kw
        })
      },
      cleanHistory() {
        this.historyList = [],
          uni.setStorageSync('kw', '[]')
      },
      search(item) {
        uni.navigateTo({
          url: '/subpkg/goods-detail/goods-detail?goods_id=' + item.goods_id
        })
      },
      input(e) {
        // e.value 是最新的搜索内容
        // console.log(e)
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e
          this.getsearchResults()
        }, 500)
      },
      // 搜索商品建议列表
      async getsearchResults() {
        if (this.kw === '') {
          this.searchResults = []
          return
        }
        const res = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        })
        console.log('搜索-------', res);
        if (res.data.meta.status != 200) return uni.$showMsg()
        this.searchResults = res.data.message

        this.saveSearchHistory()
      },
      // 搜索内容push到搜索历史中
      saveSearchHistory() {
        // this.historyList.push(this.kw)
        // 1. 将 Array 数组转化为 Set 对象
        const set = new Set(this.historyList)
        console.log(set);
        // 2. 调用 Set 对象的 delete 方法，移除对应的元素(把搜索历史中的这个字段移除)
        set.delete(this.kw)
        // 3. 调用 Set 对象的 add 方法，向 Set 中添加元素
        set.add(this.kw)
        // 4. 将 Set 对象转化为 Array 数组
        this.historyList = Array.from(set)
        // 持久化存储
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      }

    },


  }
</script>

<style lang="scss">
  .search-box {
    background-color: aqua;
    position: sticky;
    top: 0px;
    z-index: 999;
  }

  .search-item {
    display: flex;
    justify-content: space-between;
    // align-items: center;
    padding: 0 5px;
    // background-color: antiquewhite;

    .goods-name {
      // background-color: aqua;
      // 文本不允许换行
      white-space: nowrap;
      // 超出去的地方用...代替
      text-overflow: ellipsis;
      // 溢出区的部分隐藏
      overflow: hidden;
      font-size: 12px;
      padding: 13px 10px;
      border-bottom: 3px solid #efefef;
      font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
      // 
    }
  }

  .history-box {
    padding: 0 10px;

    .history-title {
      height: 40px;
      line-height: 40px;
      font-size: 13px;
      border-bottom: 2px solid #efefef;

      .uni-icons {
        float: right;
      }
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;
      margin-top: 5px;

      .uni-tag {
        margin-left: 5px;
        background-color: #efefef;
        color: black;
        margin-top: 10px;
      }
    }
  }
</style>
